package com.studentexam.student.service.impl;

import com.studentexam.student.dao.AnswerMapper;
import com.studentexam.student.dao.ExamMapper;
import com.studentexam.student.dao.QuestionBankMapper;
import com.studentexam.student.dao.TeacherMapper;
import com.studentexam.student.model.Teacher;
import com.studentexam.student.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private QuestionBankMapper questionBankMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private ExamMapper examMapper;


    @Override
    public int addTeacher(Teacher teacher) {
        int returnInt=teacherMapper.insertTeacher(teacher);

        return returnInt;
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        int isUpdate=0;
        try{
            isUpdate=teacherMapper.updateTeacher(teacher);
        }catch (Exception e){
            isUpdate=-1;
            System.out.println("更新角色时执行sql报错原因为:"+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int delTeacher(int teacherid) {
        int returnDel=0;
        try {
            if (questionBankMapper.getTeacherCount(teacherid)< 1&answerMapper.getTeacherCount(teacherid)<1&examMapper.getTeacherCount(teacherid)<1) {
                returnDel = teacherMapper.delTeacher(teacherid);
            }
        }catch (Exception e){
            returnDel=-1;
            System.out.println("删除失败原因为："+e.getMessage());
        }
        return returnDel;
    }

    @Override
    public List<Teacher> getByTeacherid(int teacherid) {
        List<Teacher> teacherList=teacherMapper.getByTeacherid(teacherid);
        return teacherList;
    }

    @Override
    public List<Teacher> getAllTeacher(Teacher teacher){
        List<Teacher> teacherList = teacherMapper.getAllTeacher(teacher);
        return teacherList;
    }
}
